home *** CD-ROM | disk | FTP | other *** search
/ Champak 140 / (Vol 140) Sep 19 2011.iso / Games / football_extravaganza.swf / scripts / DefineSprite_140 / frame_1 / DoAction.as
Text File  |  2011-09-19  |  10KB  |  396 lines

  1. function newMatch()
  2. {
  3.    start_whist = false;
  4.    end_whist = false;
  5.    time = getTimer();
  6.    start_time = 0;
  7.    match_end = false;
  8.    left_score = 0;
  9.    right_score = 0;
  10.    extratime = false;
  11.    running = false;
  12. }
  13. function centre()
  14. {
  15.    ball._x = clip_width / 2;
  16.    ball._y = clip_height / 2;
  17.    left._x = 40;
  18.    left._y = clip_height / 2;
  19.    right._x = clip_width - 40;
  20.    right._y = clip_height / 2;
  21. }
  22. function start_game()
  23. {
  24.    ball_xmov = (Math.random() * ball_speed_diff + ball_speed_max) * (ball_side != "R" ? 1 : -1);
  25.    ball_ymov = (Math.random() * ball_speed_diff + ball_speed_max) * (0.5 >= Math.random() ? 1 : -1);
  26.    _parent.onKick();
  27.    if(extratime)
  28.    {
  29.       over_whist = false;
  30.    }
  31.    if(start_time == 0)
  32.    {
  33.       start_time = getTimer();
  34.    }
  35.    running = true;
  36. }
  37. function start_extratime()
  38. {
  39.    match_duration += extratime_duration;
  40.    extratime = true;
  41. }
  42. function stop_game()
  43. {
  44.    running = false;
  45.    time = getTimer();
  46. }
  47. function move_ball()
  48. {
  49.    if(!match_end && start_time != 0)
  50.    {
  51.       s = Math.round((getTimer() - start_time) / 1000,0);
  52.       m = Math.floor(s / 60,0);
  53.       if(m == 0)
  54.       {
  55.          time_disp = "00:" + leftPadding(s,2,"0");
  56.       }
  57.       else
  58.       {
  59.          time_disp = leftPadding(m,2,"0") + ":" + leftPadding(s - m * 60,2,"0");
  60.       }
  61.    }
  62.    if(!running & !match_end)
  63.    {
  64.       if(2000 < getTimer() - time && getTimer() - time < 2500)
  65.       {
  66.          centre();
  67.          start_whist = false;
  68.       }
  69.       if(2500 < getTimer() - time && !start_whist)
  70.       {
  71.          _parent.onKickOff();
  72.          start_whist = true;
  73.       }
  74.       if(3000 < getTimer() - time)
  75.       {
  76.          start_game();
  77.       }
  78.    }
  79.    else
  80.    {
  81.       if(match_duration < getTimer() - start_time)
  82.       {
  83.          match_end = true;
  84.          stop_game();
  85.          if(!over_whist)
  86.          {
  87.             _parent.onGameOver(left_score == 0 && 0 < right_score && !extratime);
  88.             over_whist = true;
  89.             if(left_score == 0 && 0 < right_score && !extratime)
  90.             {
  91.                match_end = false;
  92.                _parent.onExtraTime();
  93.                start_extratime();
  94.             }
  95.          }
  96.          return undefined;
  97.       }
  98.       ball._x += ball_xmov;
  99.       ball._y += ball_ymov;
  100.       ballx = ball._x;
  101.       bally = ball._y;
  102.       if(clip_width - ball._width / 2 < ballx)
  103.       {
  104.          ball._x = clip_width - ball._width / 2;
  105.          ball_xmov *= -1;
  106.          if(isGoal())
  107.          {
  108.             left_score++;
  109.             stop_game();
  110.             _parent.onRightGoal();
  111.             ball._x += ball_xmov * -2;
  112.             ball._y += ball_ymov * 2;
  113.             return undefined;
  114.          }
  115.          _parent.onHitWall(1);
  116.       }
  117.       if(ballx < ball._width / 2)
  118.       {
  119.          ball._x = ball._width / 2;
  120.          ball_xmov *= -1;
  121.          if(isGoal())
  122.          {
  123.             right_score++;
  124.             stop_game();
  125.             ball._x += ball_xmov * -2;
  126.             ball._y += ball_ymov * 2;
  127.             _parent.onLeftGoal();
  128.             return undefined;
  129.          }
  130.          _parent.onHitWall(-1);
  131.       }
  132.       if(clip_height - ball._height / 2 < bally)
  133.       {
  134.          ball._y = clip_height - ball._height / 2;
  135.          ball_ymov *= -1;
  136.          _parent.onHitWall(0);
  137.       }
  138.       if(bally < ball._height / 2)
  139.       {
  140.          ball._y = ball._height / 2;
  141.          ball_ymov *= -1;
  142.          _parent.onHitWall(0);
  143.       }
  144.       if(ball.hitTest(left) && ball_xmov < 0)
  145.       {
  146.          ball_xmov *= -1;
  147.          ball._x = left._x + left._width / 2 + ball._width / 2;
  148.          _parent.onKick(-1);
  149.          return undefined;
  150.       }
  151.       if(ball.hitTest(right) && 0 < ball_xmov)
  152.       {
  153.          ball_xmov *= -1;
  154.          ball._x = right._x - (right._width / 2 + ball._width / 2 - Math.abs(ball_xmov));
  155.          left._x = 10 + Math.ceil(Math.random() * right._x / 3);
  156.          left_mov = Math.sqrt(left_mov * left_mov) * (left._y >= left_targety ? -1 : 1);
  157.          _parent.onKick(1);
  158.          return undefined;
  159.       }
  160.       ball.football._rotation += 20 * sign(ball_ymov);
  161.       leftMove();
  162.    }
  163. }
  164. function leftHit(x, y)
  165. {
  166.    if(ball_xmov < 0)
  167.    {
  168.       if(left._y - left._height / 2 < y && y < left._y + left._height / 2 && x < left._x + left._width && left._x - left._width - ball._width / 2 < x)
  169.       {
  170.          return true;
  171.       }
  172.    }
  173.    return false;
  174. }
  175. function rightHit(x, y)
  176. {
  177.    if(0 < ball_xmov)
  178.    {
  179.       if(right._y - right._height / 2 < y && y < right._y + right._height / 2 && right._x - right._width / 2 < x && x < right._x + right._width / 2)
  180.       {
  181.          return true;
  182.       }
  183.    }
  184.    return false;
  185. }
  186. function isGoal()
  187. {
  188.    txt = 100;
  189.    var x = ball._x - ball._width / 2;
  190.    var y = ball._y;
  191.    var goal_top = clip_height / 2 - goal_width / 2;
  192.    var goal_bottom = clip_height / 2 + goal_width / 2;
  193.    if(y >= goal_top && goal_bottom >= y)
  194.    {
  195.       return true;
  196.    }
  197.    return false;
  198. }
  199. function rightMove(xdirection, ydirection)
  200. {
  201.    var right_top;
  202.    var right_left;
  203.    var right_bottom;
  204.    var right_right;
  205.    var movie_top = 0;
  206.    var movie_bottom = clip_height;
  207.    var movie_left = 0;
  208.    var movie_right = clip_width;
  209.    if(running)
  210.    {
  211.       right._y += right_speed * ydirection;
  212.       right._x += right_speed * xdirection;
  213.       right_top = right._y - right._height / 2;
  214.       right_bottom = right._y + right._height / 2;
  215.       right_left = right._x - right._width / 2;
  216.       right_right = right._x + right._width / 2;
  217.       if(right_top < movie_top)
  218.       {
  219.          right._y = right._height / 2;
  220.       }
  221.       if(movie_bottom < right_bottom)
  222.       {
  223.          right._y = movie_bottom - right._height / 2;
  224.       }
  225.       if(movie_right < right_right)
  226.       {
  227.          right._x = movie_right - right._width / 2;
  228.       }
  229.       if(right_left < movie_left)
  230.       {
  231.          right._x = right._width / 2;
  232.       }
  233.    }
  234. }
  235. function rightMove_mouse(x, y)
  236. {
  237.    var right_top;
  238.    var right_left;
  239.    var right_bottom;
  240.    var right_right;
  241.    var movie_top = 0;
  242.    var movie_bottom = clip_height;
  243.    var movie_left = 0;
  244.    var movie_right = clip_width;
  245.    right._y = y;
  246.    right_top = right._y - right._height / 2;
  247.    right_bottom = right._y + right._height / 2;
  248.    right_left = right._x - right._width / 2;
  249.    right_right = right._x + right._width / 2;
  250.    if(right_top < movie_top)
  251.    {
  252.       right._y = right._height / 2;
  253.    }
  254.    if(movie_bottom < right_bottom)
  255.    {
  256.       right._y = movie_bottom - right._height / 2;
  257.    }
  258.    if(movie_right < right_right)
  259.    {
  260.       right._x = movie_right - right._width / 2;
  261.    }
  262.    if(right_left < movie_left)
  263.    {
  264.       right._x = right._width / 2;
  265.    }
  266. }
  267. function leftMove()
  268. {
  269.    if(running)
  270.    {
  271.       if(ball_xmov < 0)
  272.       {
  273.          if(ball_side == "R")
  274.          {
  275.             left_targety = getLeftHitPoint(ball._x,ball._y,ball_xmov,ball_ymov);
  276.             left_mov = Math.sqrt(left_mov * left_mov) * (left._y >= left_targety ? -1 : 1);
  277.          }
  278.          ball_side = "L";
  279.          if(!leftHit(left._x,left_targety))
  280.          {
  281.             left._y += left_mov;
  282.          }
  283.       }
  284.       else
  285.       {
  286.          ball_side = "R";
  287.          ball_xmove += 0.3 * (0.5 >= Math.random() ? -1 : 1);
  288.          ball_ymov += 0.3 * (0.5 >= Math.random() ? -1 : 1);
  289.          if(ball_xmov * sign(ball_xmov) < ball_ymov * sign(ball_ymov))
  290.          {
  291.             ball_ymov *= 0.75;
  292.          }
  293.       }
  294.    }
  295. }
  296. function getLeftHitPoint(x, y, xmov, ymov)
  297. {
  298.    hitx = 0;
  299.    hity = 0;
  300.    left_guess_count = 0;
  301.    while(left._x < x)
  302.    {
  303.       left_guess_count++;
  304.       if(left_guess_power < left_guess_count)
  305.       {
  306.          break;
  307.       }
  308.       x += xmov;
  309.       y += ymov;
  310.       hitx = x;
  311.       hity = y;
  312.       if(clip_width - ball._width / 2 < hitx)
  313.       {
  314.          x = clip_width - ball._width / 2;
  315.          xmov *= -1;
  316.       }
  317.       if(hitx < ball._width / 2)
  318.       {
  319.          x = ball._width / 2;
  320.          xmov *= -1;
  321.       }
  322.       if(clip_height - ball._height / 2 < hity)
  323.       {
  324.          y = clip_height - ball._height / 2;
  325.          ymov *= -1;
  326.       }
  327.       if(hity < ball._height / 2)
  328.       {
  329.          y = ball._height / 2;
  330.          ymov *= -1;
  331.       }
  332.    }
  333.    return y;
  334. }
  335. function sign(n)
  336. {
  337.    if(n < 0)
  338.    {
  339.       return -1;
  340.    }
  341.    if(0 < n)
  342.    {
  343.       return 1;
  344.    }
  345.    if(n == 0)
  346.    {
  347.       return 0;
  348.    }
  349. }
  350. function leftPadding(str, n, paddingChr)
  351. {
  352.    var i;
  353.    str += "";
  354.    padding = "";
  355.    i = 0;
  356.    while(i < n - str.length)
  357.    {
  358.       padding += paddingChr;
  359.       i++;
  360.    }
  361.    return padding + str;
  362. }
  363. var clip_height = this._height;
  364. var clip_width = this._width;
  365. var goal_width = 178.6 - 2 * ball._height;
  366. var ball_speed_diff = 2;
  367. var ball_speed_max = 5;
  368. var ball_xmov;
  369. var ball_ymov;
  370. var right_speed = 40;
  371. var ball_side = "R";
  372. var left_targety = 0;
  373. var left_speed = 3;
  374. var left_mov = 2;
  375. var left_guess_power = 200;
  376. var left_guess_count = 0;
  377. var left_score = 0;
  378. var right_score = 0;
  379. var start_whist = false;
  380. var over_whist = false;
  381. var time = getTimer();
  382. var start_time = 0;
  383. var match_duration = 150000;
  384. var extratime_duration = 60000;
  385. var match_end = false;
  386. var running = false;
  387. var extratime = false;
  388. var crowd_sound_hush;
  389. if(!crowd_sound_hush)
  390. {
  391.    crowd_sound_hush = new Sound(this);
  392.    crowd_sound_hush.attachSound("crowd_sound_hush");
  393.    crowd_sound_hush.start(0,10000);
  394. }
  395. time_disp = "00:00";
  396.